
#include "de_image.h"


//****************************************************************

static d_image_para_t d_p;

//****************************************************************



//****************************************************************
int de_image_file(const char *file_name)
{
	int i    = 0;
	int ret  = 0;
	int hours, mins, secs, us;
	//****************************************************************

	d_p.f_avformat = avformat_alloc_context();
	if(!d_p.f_avformat){
		log_e("avformat error\n");
		return -1;
	}
	//****************************************************************

	ret = avformat_open_input(&d_p.f_avformat, file_name, NULL,NULL);
	if(ret != 0){
		log_e("open err=%s\n",file_name);
		avformat_free_context(d_p.f_avformat);
		d_p.f_avformat = NULL;
		return -1;
	}
	//****************************************************************
	ret = avformat_find_stream_info(d_p.f_avformat, NULL);
	if(ret < 0){	
		log_e("no stream\n");

		avformat_close_input(&d_p.f_avformat);
		avformat_free_context(d_p.f_avformat);
		d_p.f_avformat = NULL;
		return -1;
	}	
	//****************************************************************
	
	log_i("************************************************************************************\n");

	av_dump_format(d_p.f_avformat,0, file_name,0);
	
	log_i("************************************************************************************\n");

	log_d("duration=%lld\n",d_p.f_avformat->duration);
	//****************************************************************

	d_p.f_v_index = -1;

	log_d("nb_streams=%d\n",d_p.f_avformat->nb_streams);
	//****************************************************************
	for (i = 0; i < d_p.f_avformat->nb_streams; i++)
	{
		log_d("codec_type=%d\n",d_p.f_avformat->streams[i]->codecpar->codec_type);
		
		if (d_p.f_avformat->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) 
		{
			d_p.f_v_index = i;
		}
	}		

	log_d("v_index =%d\n",d_p.f_v_index);
	//*************************************************************************************
	
	d_p.f_duration = d_p.f_avformat->duration;

	secs  	 = d_p.f_duration / AV_TIME_BASE;
	us	     = d_p.f_duration % AV_TIME_BASE;
	mins     = secs / 60;
	secs    %= 60;
	hours    = mins / 60;
	mins     %= 60;
	
	log_d("duration=%lld\n",d_p.f_duration);
	log_d("duration=%02d:%02d:%02d.%02d\n",hours,mins,secs,(100 * us) / AV_TIME_BASE);
	//*************************************************************************************
	if(d_p.f_v_index == -1)
	{
		log_e("no video stream\n");		

		avformat_close_input(&d_p.f_avformat);
		avformat_free_context(d_p.f_avformat);
		d_p.f_avformat = NULL;
		
		return -1;
	}
	//*************************************************************************************
	log_d("v_codec_id =%d\n",d_p.f_avformat->streams[d_p.f_v_index]->codecpar->codec_id);
	//*************************************************************************************

    d_p.f_v_ctx = avcodec_alloc_context3(NULL);
    if(!d_p.f_v_ctx){
		log_e("avcodec alloc error\n");
		return -1;
	}

    ret = avcodec_parameters_to_context(d_p.f_v_ctx, d_p.f_avformat->streams[d_p.f_v_index]->codecpar);
    if (ret < 0){
		log_e("avcodec parameters error\n");
		return -1;
	}
	//*************************************************************************************

    d_p.f_v_codec = avcodec_find_decoder(d_p.f_v_ctx->codec_id);
	if(d_p.f_v_codec){
		log_d("v name =%s\n",d_p.f_v_codec->name);
	}
	//*************************************************************************************

	d_p.f_v_w  = d_p.f_v_ctx->width;
	d_p.f_v_h  = d_p.f_v_ctx->height;

	log_d("v_w =%d\n",d_p.f_v_w);
	log_d("v_h =%d\n",d_p.f_v_h);
	//*************************************************************************************
	d_p.f_v_stream = d_p.f_avformat->streams[d_p.f_v_index];
	d_p.f_fps      = d_p.f_v_stream->avg_frame_rate.num/d_p.f_v_stream->avg_frame_rate.den;

	log_d("fps =%d\n",d_p.f_fps);
	//*************************************************************************************

	d_p.f_vpacket = av_packet_alloc();
	if(!d_p.f_vpacket){
		log_e("v packet error.\n");
		return -1;
	}
	av_init_packet(d_p.f_vpacket);
	//****************************************************************

	log_d("open file stream succeed.\n");
	//****************************************************************

}
//****************************************************************
int de_image_close(void)
{
	log_d("close file stream.\n");
	
	if(d_p.f_v_ctx){
		avcodec_free_context(&d_p.f_v_ctx);
		d_p.f_v_ctx = NULL;
	}
	if(d_p.f_v_ctx){
		avcodec_close(d_p.f_v_ctx);
		d_p.f_v_ctx = NULL;
	}
	//***************************************

	if(d_p.f_vpacket){
		av_packet_free(d_p.f_vpacket);
		d_p.f_vpacket = NULL;
	}
	//***************************************

	if(d_p.f_avformat)
	{
		if(&d_p.f_avformat)
			avformat_close_input(&d_p.f_avformat);
		avformat_free_context(d_p.f_avformat);
		d_p.f_avformat = NULL;
	}
	//***************************************

	log_d("close file stream succeed.\n");
	
	return 0;

}




